package com.gm.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gm.bean.Admin;
import com.gm.bean.Book;
import com.gm.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class BookController {
    @Autowired
    BookMapper bookMapper;
    @GetMapping("/books")
    public String findAll(@RequestParam(defaultValue = "1") Integer page,
                          @RequestParam(defaultValue = "5") Integer size,
                          Model model){
        PageHelper.startPage(page,size);
        List<Book> bookList = this.bookMapper.findBookAll(page,size);
        PageInfo<Book> pageInfo = new PageInfo<>(bookList);
        model.addAttribute("books",pageInfo);
        return "book-list";
    }
    @PostMapping ("/book")
    public String bookAdd(Book book){
//        System.out.println(book);
        this.bookMapper.AddBook(book);
        System.out.println(book);
        return "redirect:/books";
    }
    @DeleteMapping("/book/{bid}")
    public String delBook(@PathVariable("bid") Integer bid){
        System.out.println("到达删除");
        System.out.println(bid);
        this.bookMapper.delBook(bid);
        System.out.println("已访问数据库");
        return "redirect:/books";
    }
    @GetMapping("/book/{bookname}")
    public String bookUpd(@PathVariable("bookname") String bookname, Model model){
        Book book = this.bookMapper.getBookByName(bookname);
        model.addAttribute("select1",book);
        System.out.println(book);
        return "book-upd";
    }
    @PutMapping("/book")
    public String bookUpd(Book book){
        this.bookMapper.bookUpd(book);
        return "redirect:/books";
    }

}
